<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$return['basic']=$this->plugin_setting('basic');
$worker=pdo_get('jy_yuyue_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
 if(!$worker && $basic['ruzhu']==1){		
	$_RQ['op']='ruzhu';
		//待审核
}
if($worker && $worker['examine']==0 && $basic['ruzhu']==1){
	$_RQ['op']='check';
}
if($_RQ['op']=='main'){
	if($return['basic']['isworker']==0){
		$worker=pdo_get('jy_yuyue_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$worker){
			exi('你还不是员工！','error','jy_yuyue/index/');
		}
		if($worker['examine']==0){
			exi('您的入驻申请还在审核中，请耐心等待！','error','jy_yuyue/index/');
		}
		if($worker['islock']==1){
			exi('您已被锁定，请联系管理员！','error','jy_yuyue/index/');
		}
		//今日订单数量
		$day=date("Y-m-d",time("day"));
		$today_num=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$worker['id'],'day'=>$day,'status'=>array(1,2,4,7)));
		//总订单数量
		$all_num=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$worker['id'],'status'=>array(1,2,4,7)));
		//总业绩
		$all_money = pdo_getsum('jy_yuyue_order',array("uniacid" => $_SESSION['uniacid'],"worker_id" => $worker['id'],'status'=>array(1,2,4,7)),'pay');
		//今日业绩
		$today_money = pdo_getsum('jy_yuyue_order',array('day'=>$day,"uniacid" => $_SESSION['uniacid'],"worker_id" => $worker['id'],'status'=>array(1,2,4,7)),'pay');
	}else{
		//查询是否为管理员
		$managers=pdo_getall('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'managerid'=>$_SESSION['uid']));
		if(empty($managers)){
			exi('您不是管理员！','error','jy_yuyue/index/');
		}
		$return['manager']=1;
		$return['member']=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('nickname','avatar'));
		$day=date("Y-m-d",time("day"));
		$today_num=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'day'=>$day,'status'=>array(1,2,4,7)));
		//总订单数量
		$all_num=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,4,7)));
		//总业绩
		$all_money = pdo_getsum('jy_yuyue_order',array("uniacid" => $_SESSION['uniacid'],'status'=>array(1,2,4,7)),'pay');
		//今日业绩
		$today_money = pdo_getsum('jy_yuyue_order',array('day'=>$day,"uniacid" => $_SESSION['uniacid'],'status'=>array(1,2,4,7)),'pay');
	}
	$return['worker']=$worker;
	$return['today_num']=$today_num;
	$return['all_num']=$all_num;
	$return['all_money']=$all_money;
	$return['today_money']=$today_money;	
	exi($return);
}
//修改员工信息
if($_RQ['op']=='set'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
	
		$data=array(
				'headimg'=>$postdata['headimg'],
				'name'=>$postdata['name'],
				'address'=>$postdata['address'],
				'phone'=>$postdata['phone'],
				);
		$worker_id=pdo_getcolumn('jy_yuyue_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');		
		if($worker_id){
			pdo_update('jy_yuyue_worker',$data,array('id'=>$worker_id));
			exi('修改成功','','jy_yuyue/worker_home/');		 
		}
	}
	$worker=pdo_get('jy_yuyue_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['worker']=$worker;
	exi($return);
	}
	
//员工入驻
if($_RQ['op']=='ruzhu'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$data=array(
				'mid'=>$_SESSION['uid'],
				'uniacid'=>$_SESSION['uniacid'],
				'name'=>$postdata['name'],
				'phone'=>$postdata['mobile'],
				'headimg'=>$postdata['headimg'],
				'address'=>$postdata['address'],
				'age'=>$postdata['age'],
				'years'=>$postdata['years'],
				'remark'=>$postdata['remark'],
				'zhengjian'=>$postdata['zhengjian'],
				'status'=>1,
				'examine'=>0,//待审核
				'createtime'=>TIMESTAMP,
				);
		$worker_id=pdo_getcolumn('jy_yuyue_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');		
		if(!$worker_id){
			pdo_insert('jy_yuyue_worker',$data);
			exi('成功提交申请！','','jy_yuyue/worker_home/');		 
		}
		else{
			pdo_update('jy_yuyue_worker',$data,array('id'=>$worker_id,'uniacid'=>$_SESSION['uniacid']));
			exi('成功提交申请！','','jy_yuyue/worker_home/');
		}
	}
	$return['worker']=array();
	exi($return);
}
//审核中
if($_RQ['op']=='check'){
	$basic==$this->plugin_setting('basic');
	$return['banner']=$basic['image'];
	exi($return);
}